﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using YXShop.IDAL.Store;
using System.Data;
using System.Data.SqlClient;

namespace YXShop.SQLServerDAL.Store
{
    public class StoreAnswer : IStoreAnswer
    {
        #region DataBase Operation

        public int Add(YXShop.Model.Store.StoreAnswer model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into yxs_storeanswer(");
            strSql.Append("askid,answerid,answertime,message)");
            strSql.Append(" values(");
            strSql.Append("@askid,@answerid,@answertime,@message)");
            strSql.Append(" Select scope_IDENTITY()");
            SqlParameter[] parameters = (SqlParameter[])this.ValueParas(model);
            object obj = ChangeHope.DataBase.SQLServerHelper.GetSingle(strSql.ToString(),parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        public int Update(YXShop.Model.Store.StoreAnswer model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update [yxs_storeanswer] set ");
            strSql.Append("askid=@askid,answerid=@answerid,answertime=@answertime,message=@message");
            strSql.Append(" where id=@id ");
            SqlParameter[] parameters = (SqlParameter[])this.ValueParas(model);
            return ChangeHope.DataBase.SQLServerHelper.ExecuteSql(strSql.ToString(), parameters);
        }

        public void Delete(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete  from [yxs_storeanswer] ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameter = { new SqlParameter("@id",SqlDbType.Int,4)};
            parameter[0].Value = id;
            ChangeHope.DataBase.SQLServerHelper.ExecuteSql(strSql.ToString(),parameter);

        }


        public void DeleteAll(string ids)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from [yxs_storeanswer] where id in(");
            strSql.Append(" "+ids+" )");
            ChangeHope.DataBase.SQLServerHelper.ExecuteSql(strSql.ToString());
        }
      
        #endregion

        #region DataBase Load

        public YXShop.Model.Store.StoreAnswer GetModelById(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from yxs_storeanswer ");
            strSql.Append(" where id=@id ");
            SqlParameter[] parameter ={
                                          new SqlParameter("@id",SqlDbType.Int,4)
                                     };
            parameter[0].Value = id;
            YXShop.Model.Store.StoreAnswer model = null;
            using(SqlDataReader reader=ChangeHope.DataBase.SQLServerHelper.ExecuteReader(strSql.ToString(),parameter))
            {
                while(reader.Read())
                {
                    model = new YXShop.Model.Store.StoreAnswer();
                    model.Id = Convert.ToInt32(reader["id"]);
                    model.AskId = Convert.ToInt32(reader["askid"]);
                    model.AnswerId = Convert.ToInt32(reader["answerid"]);
                    model.AnswerTime = Convert.ToDateTime(reader["answertime"]);
                    model.Message = (string)reader["message"];
                }
            }
            return model;
        }

        public List<YXShop.Model.Store.StoreAnswer> GetListStoreAnswerByWhere(string where)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from yxs_storeanswer ");
            if(where!=null&&where!="")
            {
                strSql.Append(" where 1=1 "+where+" ");
            }
            List<YXShop.Model.Store.StoreAnswer> storeList = new List<YXShop.Model.Store.StoreAnswer>();
            using (SqlDataReader reader = ChangeHope.DataBase.SQLServerHelper.ExecuteReader(strSql.ToString()))
            {
                while (reader.Read())
                {
                    YXShop.Model.Store.StoreAnswer model = new YXShop.Model.Store.StoreAnswer();
                    model.Id = Convert.ToInt32(reader["id"]);
                    model.AskId = Convert.ToInt32(reader["askid"]);
                    model.AnswerId = Convert.ToInt32(reader["answerid"]);
                    model.AnswerTime = Convert.ToDateTime(reader["answertime"]);
                    model.Message = (string)reader["message"];
                    storeList.Add(model);
                }
            }
            return storeList;
        }
        public ChangeHope.DataBase.DataByPage GetList(string strWhere)
        {
            ChangeHope.DataBase.DataByPage dataPage = new ChangeHope.DataBase.DataByPage();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("[select] answer.*,admin.name [from] yxs_storeanswer answer left join yxs_administrators admin on answer.answerid=admin.adminid [where] 1=1 ");
            if(strWhere!=null&&strWhere!="")
            {
                strSql.Append(""+strWhere);
            }
            strSql.Append(" [order by] id desc");
            dataPage.Sql = strSql.ToString();
            dataPage.GetRecordSetByPage();
            return dataPage;
        }

        /// <summary>
        /// 排序所有记录
        /// </summary>
        /// <remarks></remarks>
        public ChangeHope.DataBase.DataByPage GetList(string orderfield, int pagesize, string Conditions)
        {
            ChangeHope.DataBase.DataByPage dataPage = new ChangeHope.DataBase.DataByPage();
            dataPage.Sql = "[select] * [from] yxs_storeanswer [where] 1=1 " + Conditions + " " + orderfield;
            dataPage.PageSize = pagesize;
            dataPage.GetRecordSetByPage();
            return dataPage;
        }

        #endregion

        #region Other Function
        public IDbDataParameter[] ValueParas(YXShop.Model.Store.StoreAnswer model)
        {

            SqlParameter[] parameters ={
                                          new SqlParameter("@id",SqlDbType.Int,4),
                                          new SqlParameter("@askid",SqlDbType.Int,4),
                                          new SqlParameter("@answerid",SqlDbType.Int,4),
                                          new SqlParameter("@answertime",SqlDbType.DateTime),
                                          new SqlParameter("@message",SqlDbType.NText),
                                      };
            parameters[0].Value = model.Id;
            parameters[1].Value = model.AskId;
            parameters[2].Value = model.AnswerId;
            parameters[3].Value = model.AnswerTime;
            parameters[4].Value = model.Message;

            return parameters;
        }
        #endregion

       
    }
}
